package com.dhecp.project.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.project.base.domain.BaseStockType;
import com.dhecp.project.base.service.BaseStockTypeService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 库存类型 控制层
 * @author DH-lwl
 * @version 2019年12月10日
 */
@Controller
@RequestMapping("/base/basestocktype")
public class BaseStockTypeController  extends BaseController{

	private String prefix = "base/basestocktype";
	private String moduleTable = ModuleTable.BASE_STOCK_TYPE;
	
	@Autowired
    private BaseStockTypeService baseStockTypeService;
	
	@Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;
    
    /**
     * 打开仓库列表
     * @param
     * @return ModelAndView
     * @author lwl
     */
	@RequiresPermissions("base:basestocktype:view")
    @GetMapping()
    public ModelAndView dataList(@RequestParam Map<String, Object> params)
    {
		params.put("moduleTable", moduleTable);
    	params.put("entity", new BaseStockType()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("toolBarWidth", 0);
		ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("moduleTable", moduleTable);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.setViewName(prefix + "/basestocktype");
		return mv;
    }
	
	/**
     * 查询
     * @param params
     * @return TableDataInfo
     * @author DH-lwl
     */
    @RequiresPermissions("base:basestocktype:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	List<BaseStockType> list = new ArrayList<BaseStockType>();
        try {
            list = baseStockTypeService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());   
        }
        return getDataTable(list);
    }
	
    /**
     * 修改
     * @param baseStockTypeItems
     * @return MessageModel
     * @author DH-lwl
     */
    @RequiresPermissions("base:basestocktype:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editPost(@RequestParam("baseStockTypeItems") String baseStockTypeItems) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(baseStockTypeItems)) {
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }
        try {
            ajaxResult= baseStockTypeService.updateBXjXc(baseStockTypeItems);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}
